// pages/opreation/cabinet/form/index.js
import {
  request
} from '../../../../request';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    form: {
      cabinetDate: "2020-01-01"
    },
    memberList: [],
    memberIndex: null,
  },
  onLoad: function (options) {
    request("/gym/member/list", "GET").then(resopnse => {
      console.log(resopnse);
      this.setData({
        memberList: resopnse.rows
      });
    })
    if (options.id != undefined) {
      this.loadFormData(options.id);
    }
  },
  loadFormData(id) {
    request("/gym/cabinet/" + id, "GET").then(res => {
      if (res.code == 200) {
        var form = res.data;
        var list = this.data.memberList;
        var index = null;
        for(var i = 0;i<list.length;i++){
          if(list[i].memberId == form.memberId){
             index = i; 
          }
        }
        this.setData({
          form: form,
          memberIndex:index
        })
      }
    })
    console.log(this.data)
  },
  PickerChange(e) {
    var data = this.data.form;
    data["memberId"] = this.data.memberList[e.detail.value].memberId;
    data["memberName"] = this.data.memberList[e.detail.value].memberName;
    this.setData({
      memberIndex: e.detail.value,
      form: data
    })
  },
  TimeChange(e) {
    this.setData({
      time: e.detail.value
    })
  },
  nameInput(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData["cabinetNo"] = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  
  formSubmit: function (e) {
    var status;
    var formData = this.data.form;
    if (formData.cabinetId != undefined) {
      status = "PUT";
    } else {
      status = "POST";
    }
    request("/gym/cabinet", status, formData).then(res => {
      if (res.code == "200") {
        wx.showToast({
          title: '保存成功',
          icon: '',
          duration: 2000,
        })
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
})